<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/12
 * Time: 16:10
 */
namespace App\Models;

Class Room_admin extends BaseModel
{

    protected $table = 'room_admin';
    public $timestamps = false;


    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     *更新数据
     */
    public function conut($where)
    {
        return $this->multiWhere($where)->count();
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    /**
     *  上房管
     */
    public function addRoomAdmin($req){
        // 判断是否为本房见主播
        $roomModel = new Roomnum();
        $is = $roomModel->isRoomZhubo($req);
        if (!$is) {
             return ['code'=>400,'msg'=>'你是非法主播,请重试'];
        }
        // 查询现有房间房管数量
        $roomadminCount = $this->conut(['room'=>$req['room']]);
        if ($roomadminCount>100){
             return ['code'=>400,'msg'=>"你只能指定100个房管"];
        }
        // 判断该用户是否已经是房管
        $where['room'] = $req['room'];
        $where['uid']  = $req['uid'];
        $roomadmininfo = $this->getOne($where);
        if ($roomadmininfo){
            return ['code'=>400,'msg'=>"该用户已经成为了你的房管"];
        }
        $insert['room'] = $req['room'];
        $insert['uid']  = $req['uid'];
        $insert['opuid'] = session('loginId');
        $res = $this->add($insert);
        if ($res) {
            return ['code'=>200,'msg'=>"添加房管成功"];
        }
        return ['code'=>400,'msg'=>"添加房管失败"];
    }

    /**
     * 下房管
     */
    public function delRoomAdmin(){

    }

    /**
     * 判断是否为房管
     */
    public function isRoomadmin($uid,$room){
         $where['uid']  = $uid;
         $where['room'] = $room;
         $res = $this->getOne($where);
         if ($res) {
              return 1;
         }
         return 0;
    }

    /**
     * 房管列表
     */
    public function roomadminlist(){
         $loginId = session('loginId');
         $roomnum = new Roomnum();
         $roominfo = $roomnum->getOne(['uid'=>$loginId]);
         $where['opuid'] = $loginId;
         $where['room']  = $roominfo['num'];
         $roomadminlist = $this->getList($where);
         if (empty($roomadminlist)) {
             return [];
         }
         $member = new Member();
         foreach ($roomadminlist as $key=>$v){
              $roomadminlist[$key]['member'] = $member->getOne(['id'=>$v['uid']]);
         }
         return ['code'=>200,'msg'=>'ok','data'=>$roomadminlist];
    }
}
